package com.xhc.demo.utils;

import com.xhc.demo.GlobalConfig;
import com.qiniu.util.Auth;
import org.json.JSONObject;
import java.util.HashMap;
import java.util.Map;

public class Utils {

   
    private static final long DOWNLOAD_URL_EXPIRES = 3600 * 5 * 5L; // 5年有效期

    // 生成上传 Token
    public static String getUploadToken() {
        Auth auth = Auth.create(GlobalConfig.QINIU_AK, GlobalConfig.QINIU_SK);
        return  auth.uploadToken("myface-mrx", null, 3600, null);
    }

    // 获取七牛云私有空间下载链接
    public static String getQiniuPrivateDownloadUrl(String key) {
        if (key == null || key.isEmpty()) {
            return "";
        }
        Auth auth = Auth.create(GlobalConfig.QINIU_AK, GlobalConfig.QINIU_SK);
        String publicUrl = String.format("%s%s", GlobalConfig.QINIU_DOMAIN, key);
        return auth.privateDownloadUrl(publicUrl, DOWNLOAD_URL_EXPIRES);
    }

    public static String getQiniuPrivateDownloadUrlThumbil(String key) {
        if (key == null || key.isEmpty()) {
            return "";
        }
        Auth auth = Auth.create(GlobalConfig.QINIU_AK, GlobalConfig.QINIU_SK);
        String publicUrl = String.format("%s%s%s", GlobalConfig.QINIU_DOMAIN, key , "?imageView2/2/w/200");
        return auth.privateDownloadUrl(publicUrl, DOWNLOAD_URL_EXPIRES);
    }

    public static String getQiniuVideoThumbil(String key) {
        if (key == null || key.isEmpty()) {
            return "";
        }
        Auth auth = Auth.create(GlobalConfig.QINIU_AK, GlobalConfig.QINIU_SK);
        String publicUrl = String.format("%s%s%s", GlobalConfig.QINIU_DOMAIN, key , "?vframe/jpg/offset/1/w/480/h/360");
        return auth.privateDownloadUrl(publicUrl, DOWNLOAD_URL_EXPIRES);
    }
    

    public static String getResponseJson(Object data){
        Map<String, Object> response = new HashMap<>();
        response.put("code", 200);
        response.put("data", data);
        return new JSONObject(response).toString();
    }

    public static String getErrorJson(int code, String message){
        Map<String, Object> response = new HashMap<>();
        response.put("code", code);
        response.put("message", message);
        return new JSONObject(response).toString();
    }
}
